/*
* TFAIImpl.h
*
* Implements TFAIInterface
*
* Author: Rapid Dojo
* Date: Oct 19, 2007
*/

#include "TFAIInterface.h"

#define OFFENSE 0
#define DEFENSE 1

//used in _myFlags and _enemyFlags
#define EMPTYFLAG -1

//for calculating board costs
//some positions can't be moved through
#define OCCUPIED -1
#define OCCUPIED_FRIENDLY_FLAG -2
#define OCCUPIED_FRIENDLY_TROOP -3
#define MAX_EXPANDED 250

#include "queue.h"
#include <string>
#include <vector>
#include <iostream>

using std::string;
using std::vector;
using std::cout;


class TFAIImpl: public TFAIInterface
{
public:

	TFAIImpl ();
	virtual ~TFAIImpl ();

	HRESULT SetupBoard(TF_PLAYERS iColor);

	HRESULT DoTurn(int iPoints, TFGameMoveList & oListOfMoves);

	HRESULT PlacePieces(TFBoard *& ioBoard, int numPoints, int behavior); //using points, place pieces on board

private:

	int _GetBehavior(); //should I act offensively or defensively
	void _UpdateBoardCost(TFBoard *& iBoard); //update movement costs for each board position
	
	//the meat of the calculations
	HRESULT _DoOffense(int iPoints, TFBoard *& oBoard, TFGameMoveList & oListOfMoves);
	HRESULT _DoOffense2(int iPoints, TFBoard *& oBoard, TFGameMoveList & oListOfMoves);
	
	HRESULT _DoDefense(int iPoints, TFBoard *& oBoard, TFGameMoveList & oListOfMoves);

	bool _firstInit;
	TFGameMoveList _myMoveList;
	TFGameMoveList _myBestList;
	int _costMoveList;
	int _depthOfMoveList;
	void GetPotentialMoves(TFGameMove &initialPosition, TFGameMove &iTarget, TFGameMoveList &oMoveList, bool useRange = true, bool returnInvalids = true);
	bool _aStarMoves;


	//My Color on the board
	//determined during SetupBoard()
	TF_PLAYERS _myColor;

	//holds the movement cost of each position on the board
	int _boardCost[BOARD_DIMENSION][BOARD_DIMENSION];
	
	//will contain EMPTYFLAG if flag was taken
	//rows = NUMBER_OF_FLAGS, columns = 2 (for x,y position on board)
	int _myFlags[NUMBER_OF_FLAGS][2]; 
	int _enemyFlags[NUMBER_OF_FLAGS][2];
	int _myTroops[NUMBER_OF_TROOPS][2]; 
	int _enemyTroops[NUMBER_OF_TROOPS][2]; 
	
	void _clearFlagPositions();



	//Intelligent functions.
	typedef  float (*hFunction) (Node* current, Node* goal);
	void A_Star(Node* init, hFunction myHeuristic, Node* goal);
	vector <Node*> Successor(Node* current);
	bool goalTest(Node* current, Node* goal);

	//Utility functions.
	
	void printSolution(Node* myNode);

	vector <Vertex*> vertex_list;					//List of all currently known verticies.
	vector<Node*>nextStates;						//Stores successor function results to be drawn for 'robot vision'.
	vector<Node*>solution_path;						//Stores solution path results.


};
